package com.zjdiepu.www.biz.icbc;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.icbc.OtherLoanLossProvisionMapper;
import com.zjdiepu.www.ibiz.icbc.IOtherLoanLossProvisionService;
import com.zjdiepu.www.model.icbc.dto.OtherLoanLossProvision;
import com.zjdiepu.www.model.icbc.ext.OtherLoanLossProvisionExt;

/**
 * other_风险准备金Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月21日 11时06分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherLoanLossProvisionServiceImpl extends BaseService implements IOtherLoanLossProvisionService {
	public static final Logger logger = LoggerFactory.getLogger(OtherLoanLossProvisionServiceImpl.class);

	@Autowired
	private OtherLoanLossProvisionMapper otherLoanLossProvisionMapper;
	
	@Override
	public void saveUpdateAndDelete(OtherLoanLossProvision otherLoanLossProvision, OperateType operType) {
		switch (operType) {
		case add:
			otherLoanLossProvision.initInsertData(otherLoanLossProvision.getModifier(), otherLoanLossProvision.getModifierName());
			otherLoanLossProvisionMapper.insert(otherLoanLossProvision);
			break;
		case edit:
			otherLoanLossProvision.initUpdateData(otherLoanLossProvision.getModifier(), otherLoanLossProvision.getModifierName());
			// 影响的记录数
			int updateCount = otherLoanLossProvisionMapper.updateByPrimaryKeySelective(otherLoanLossProvision);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherLoanLossProvision.initDeleteData(otherLoanLossProvision.getModifier(), otherLoanLossProvision.getModifierName());
			// 影响的记录数
			int deleteCount = otherLoanLossProvisionMapper.updateByPrimaryKeySelective(otherLoanLossProvision);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherLoanLossProvision> selectByExt(OtherLoanLossProvisionExt otherLoanLossProvisionExt) {
		return otherLoanLossProvisionMapper.selectByExt(otherLoanLossProvisionExt);
	}
	
	@Override
	public int countByExt(OtherLoanLossProvisionExt otherLoanLossProvisionExt){
		return otherLoanLossProvisionMapper.countByExt(otherLoanLossProvisionExt);
	}
	
	@Override
	public OtherLoanLossProvision selectById(String id) {
		return otherLoanLossProvisionMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherLoanLossProvisionMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherLoanLossProvision otherLoanLossProvision) {
		return otherLoanLossProvisionMapper.countRefined(otherLoanLossProvision);
	}

	@Override
	public OtherLoanLossProvision selectRefined(OtherLoanLossProvision otherLoanLossProvision) {
		return otherLoanLossProvisionMapper.selectRefined(otherLoanLossProvision);
	}
	
	@Override
	public int deletedRefined(OtherLoanLossProvision otherLoanLossProvision) {
		return otherLoanLossProvisionMapper.deletedRefined(otherLoanLossProvision);
	}

	@Override
	public boolean repeat(OtherLoanLossProvision otherLoanLossProvision) {
		int count = countRefined(otherLoanLossProvision);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherLoanLossProvision.getId())) {
			return true;
		}
		OtherLoanLossProvision dbItem = selectRefined(otherLoanLossProvision);
		if(null == dbItem) {
			return false;
		}
		return !otherLoanLossProvision.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherLoanLossProvisionMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherLoanLossProvision> otherLoanLossProvisions) {
		return otherLoanLossProvisionMapper.insertBatch(otherLoanLossProvisions);
	}
}
